package cn.lan.generator.service.impl;

import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreateMapperXML;
import cn.lan.generator.utils.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lanping
 * @version 1.0
 * @date 2019-07-06
 **/
public class CreateMapperXmlImpl implements CreateMapperXML {

    /**
     * 写文件
     *
     * @param entityInfo 实体信息
     * @param projInfo   项目信息
     */
    @Override
    public void writeFile(EntityInfo entityInfo, ProjInfo projInfo,String schema) throws Exception {
        String entityName = entityInfo.getEntityName();
        String tableComment = entityInfo.getTableComment();
        String tableName = entityInfo.getTableName();
        String upperName = StringUtil.toUpperCaseFirstOne(entityName);

        String mapperPackage = ConfigUtil.getMapperPackage(projInfo);
        String xmlPackage = ConfigUtil.getMappingsPackage(projInfo);

        String srcFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+Constant.TEMPLATE_MAPPER_XML_PATH;
        String suffixPath = xmlPackage.replaceAll("\\.", "\\\\") + "\\" + upperName + "Mapper" + Constant.FILE_SUFFIX_XML;
        String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getService(), Constant.PathType.basePath, suffixPath);

        String xml = writeSQLXML(entityInfo, projInfo, entityInfo.getDbType(), schema);

        Map<String,String> map = new HashMap<String,String>(){
            {
                put("tableComment",tableComment);
                put("tableName",tableName);
                put("mapperPackage",mapperPackage);
                put("upperEntityName",entityName);
                put("lowerPrimaryKey",entityInfo.getPrimaryKey());
                put("sqlXml",xml);
            }
        };
        FileUtil.copyFile(projInfo,srcFilePath, destFilePath, map);
    }


    /**
     * 写文件
     *
     * @param entityInfo 实体信息
     * @param projInfo   项目信息
     */
    public String writeSQLXML(EntityInfo entityInfo, ProjInfo projInfo, String dbType, String schema) throws Exception {
        String basePackage = projInfo.getPackageName()+"."+projInfo.getModuleName();
        String voPackage = basePackage + "." + ConfigUtil.getConfigValue(Constant.KEY_VO_PATH);
        String entityVo = entityInfo.getEntityName()+"Vo";

        StringBuffer sb = new StringBuffer();

        String model = entityInfo.getTableName();
        String schemaName = "";
        if (dbType.equals(Constant.DB_TYPE_DB2)) {
            model = "<include refid=\"schema_name\"/>";
            schemaName = "\n\t<sql id=\"schema_name\">" + schema + "." + entityInfo.getTableName() + "</sql>\n\n";
        }

        sb.append(schemaName);
        //查询单表
        sb.append(MapperXmlFileUtil.selectEntitySQL(entityInfo,model,dbType));
        //查询条件
        sb.append(MapperXmlFileUtil.queryWhereCond(entityInfo,dbType));
        sb.append(MapperXmlFileUtil.selectPageList(entityInfo,entityVo,voPackage,dbType,model));
        sb.append(MapperXmlFileUtil.selectListCountSQL(model));
        sb.append(MapperXmlFileUtil.selectByIdSQL(entityInfo,voPackage,entityVo,dbType));
        sb.append(MapperXmlFileUtil.updateSQLByMap(entityInfo, model,dbType));
        sb.append(MapperXmlFileUtil.deleteSQL(entityInfo, model,dbType));
        return sb.toString();
    }

}
